<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resources/css/public.css" media="all"/>
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all"/>
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all"/>
</head>
<body class="childrenBody">
<!-- 查询条件开始-->
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="goodsname" id="goodsname" class="layui-input searchVal"
                               placeholder="请输入商品名称"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="doSearch" shiro:hasPermission="goods:view"><span
                            class="layui-icon layui-icon-search"></span>查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span
                            class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="goodsTable" lay-filter="goodsTable"></table>
    <!-- 工具栏 -->
    <div id="goodsToolBar" style="display: none;">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="goods:create">
            <span class="layui-icon layui-icon-add-1"></span>添加商品
        </button>
    </div>

    <div id="goodsRowBar" style="display: none;">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="update" shiro:hasPermission="goods:update"><span
                class="layui-icon layui-icon-edit"></span>更新
        </button>
        <button type="reset" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete" shiro:hasPermission="goods:delete"><span
                class="layui-icon layui-icon-delete"></span>删除
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">父级分类</label>
            <div class="layui-input-block">
                <!-- 隐藏域，保存选中的部门编号 -->
                <input type="hidden" name="typeid" id="typeid">
                <ul id="goodsTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <!-- 商品编号 -->
            <input type="hidden" name="id">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsname" lay-verify="required" autocomplete="off" placeholder="请输入商品名称"
                       class="layui-input searchVal">
            </div>
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline">
                <select id="providerid" name="providerid">
                    <option value="0">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品产地</label>
            <div class="layui-input-inline">
                <input type="text" name="produceplace" lay-verify="required" autocomplete="off" placeholder="请输入商品产地"
                       class="layui-input searchVal">
            </div>
            <label class="layui-form-label">商品规格</label>
            <div class="layui-input-inline">
                <input type="text" name="size" lay-verify="required" autocomplete="off" placeholder="请输入商品规格"
                       class="layui-input searchVal">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">包装</label>
            <div class="layui-input-inline">
                <input type="text" name="goodspackage" lay-verify="required" autocomplete="off" placeholder="请输入商品单位"
                       class="layui-input searchVal">
            </div>
            <label class="layui-form-label">预警数量</label>
            <div class="layui-input-inline">
                <input type="text" name="dangernum" lay-verify="required" autocomplete="off" placeholder="请输入预警数量"
                       class="layui-input searchVal">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品售价</label>
            <div class="layui-input-inline">
                <input type="text" name="price" lay-verify="required" autocomplete="off" placeholder="请输入商品售价"
                       class="layui-input searchVal">
            </div>
            <label class="layui-form-label">商品数量</label>
            <div class="layui-input-inline">
                <input type="text" name="number" lay-verify="required" autocomplete="off" placeholder="请输入商品数量"
                       class="layui-input searchVal">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生产批号</label>
            <div class="layui-input-inline">
                <input type="text" name="productcode" lay-verify="required" autocomplete="off" placeholder="请输入生产批号"
                       class="layui-input searchVal">
            </div>
            <label class="layui-form-label">批准文号</label>
            <div class="layui-input-inline">
                <input type="text" name="promitcode" lay-verify="required" autocomplete="off" placeholder="请输入批准文号"
                       class="layui-input searchVal">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="description"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">

    var tableIns;
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery', 'form', 'table', 'layer', 'dtree'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var dtree = layui.dtree;

        //渲染数据表格
        tableIns = table.render({
            elem: '#goodsTable'//绑定表格组件的id元素
            , url: '/bus/goods/goodslist'//数据发送的请求路径
            , toolbar: '#goodsToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '商品数据表'
            , height: 'full-100'
            , page: true
            , cols: [
                [
                {field: 'id', title: 'ID', align: 'center'}
                , {field: 'goodsname', title: '商品名称', align: 'center', width: 100}
                , {field: 'produceplace', title: '商品产地', align: 'center', width: 150}
                , {field: 'size', title: '商品规格', align: 'center', width: 100}
                , {field: 'supplierName', title: '供应商', align: 'center', width: 200}
                , {field: 'goodspackage', title: '包装', align: 'center', width: 100}
                , {field: 'goodsTypeName', title: '分类名称', align: 'center', width: 100}
                , {field: 'price', title: '商品售价', align: 'center', width: 100}
                , {field: 'number', title: '商品数量', align: 'center', width: 100}
                , {field: 'dangernum', title: '预警数量', align: 'center', width: 100}
                , {fixed: 'right', title: '操作', toolbar: "#goodsRowBar", align: 'center', width: 200}
                ]
            ]
            //回调函数
            , done: function (res, curr, count) {
                // 判断当前页码是否大于1
                if (curr > 1 && res.data.length == 0) {
                    //当前页码-1
                    var pageValue = curr - 1;
                    tableIns.reload({
                        page: {curr: pageValue}// 修改页码
                    })
                }
            }
        });
        //监听模糊查询按钮点击事件
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
        });
        //头部工具栏监听事件
        table.on("toolbar(goodsTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddGoodsWindow();
                    break;
            }
        });
        //初始化父级部门的下拉菜单树
        var goodsSelectTree = dtree.renderSelect({
            width: "100%",
            elem: "#goodsTree",
            url: "/bus/goods/loadGoodsTreeLeft",
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            response: {message: "msg", statusCode: 0},  //修改response中返回数据的定义
            dataFormat: "list"
        });
        //弹出层索引，提交路径
        var mainIndex, url;

        //打开添加部门窗口
        function openAddGoodsWindow() {
            mainIndex = layer.open({
                type: 1,
                title: "添加商品",
                area: ["800px", "550px"],
                content: $("#addOrUpdateDiv"),//引用的内容窗口
                success: function () {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    //添加的提交请求
                    url = "/bus/goods/addGoods";
                }
            });
            $.get("/bus/goods/allSupplier", function (s) {
                $.each(s, function (i, v) {//遍历方法
                    $("#providerid").append("<option value='" + v.id + "'>" + v.name + "</option>")
                });
                form.render();
            });
            form.render();
        }

        //监听下拉树的选中事件
        dtree.on("node(goodsTree)", function (obj) {
            //将选中的部门id赋值给隐藏域，用于保存父级部门id
            $("#typeid").val(obj.param.nodeId);
        });

        //监听表单的提交事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (result) {
                if (result.success) {
                    //刷新数据表格
                    tableIns.reload();
                    //刷新下拉菜单树
                    goodsSelectTree.reload();
                }
                //显示提示信息
                layer.msg(result.message);
                //关闭模态框
                layer.close(mainIndex);
            }, "json");
        });
        //监听行工具栏事件
        table.on("tool(goodsTable)", function (obj) {
            switch (obj.event) {
                case 'update':
                    openUpdateGoodsWindow(obj.data);
                    break;
                case 'delete':
                    deleteGoods(obj.data);
                    break;
            }
        });

        //修改商品
        function openUpdateGoodsWindow(data) {
            mainIndex = layer.open({
                type: 1,
                title: "更新商品",
                area: ["800px", "550px"],
                content: $("#addOrUpdateDiv"),//引用的内容窗口
                success: function () {
                    $.get("/bus/goods/allSupplier", function (s) {
                        $.each(s, function (i, v) {//遍历方法
                            $("#providerid").append("<option value='" + v.id + "'>" + v.name + "</option>")
                        });
                        form.val("dataFrm", data);
                    });
                    //表单数据回显
                    //树节点回显
                    dtree.dataInit("goodsTree", data.typeid);//父节点
                    dtree.setSelectValue("goodsTree");
                    //添加的提交请求
                    url = "/bus/goods/updateGoods";
                }
            });
        }

        /**
         * 删除商品
         * @param data
         */
        function deleteGoods(data) {
            layer.confirm("确定要删除这条数据吗?", {"icon": 3, "title": "提示"}, function (index) {
                $.post("/bus/goods/deleteById", {"id": data.id}, function (result) {
                    if (result.success) {
                        //刷新表格
                        tableIns.reload();
                    }
                    //显示提示信息
                    layer.msg(result.message);
                }, "json");
                //关闭弹框
                layer.close(index);
            })
        }
    });

    /**
     * 加载数据表格的方法
     * @param goodsId
     */
    function loadTableData(goodsId) {
        tableIns.reload({
            where: {"typeid": goodsId},
            page: {
                curr: 1
            }
        });
    }

</script>
</body>
</html>